import Taro, { Component } from '@tarojs/taro'
import { login } from '../crossplatform/apiservice/login'
import { wxLoginRequest, refreshTokenRequest } from '../service/login-controller'
import { getSystemInfo } from '../crossplatform/apiservice/systemInfo'
import { setStorageSync } from '../crossplatform/apiservice/storage'
import { getArchiveInfoRequest } from '../service/user-info-controller'
import { showToast } from '../crossplatform/apiservice/toast'

const wxLogin = {
  doLogin: () => {
    return new Promise(resolve => {
      // 登录逻辑
      const { role, userId, refreshToken } = wxLogin.props
      wxLogin.setPhoneModal()
      wxLogin.goLoginFunc(resolve)
      // if (role == 'company') {
      //   // 选择为招聘者
      //   if (!userId) {
      //   // 不存在userId肯定要去走登录
      //     wxLogin.goLoginFunc(resolve)
      //   } else {
      //     // wxLogin.refreshToken(refreshToken).then(() => {
      //     resolve()
      //     // })
      //   }
      // } else {
      //   // 选择为求职者
      //   if (!userId) {
      //   // 不存在userId肯定要去走登录
      //     wxLogin.goLoginFunc(resolve)
      //   } else {
      //     // wxLogin.refreshToken(refreshToken).then(() => {
      //     resolve()
      //     // })
      //   }
      // }
    })
  },
  goLoginFunc: resolve => {
    login().then(({ code }) => {
      if (code) {
        wxLoginRequest({ code }).then(({ data = {} } = {}) => {
          const { openId, userId, mobile, access_token: accessToken, refresh_token: refreshToken } = data.data || {}
          wxLogin.props.updateUserState({
            userId,
            openId,
            mobile,
            accessToken,
            refreshToken,
            // zhichuUserId: userId
          })
          if (userId) {
            // 存在anthUserId
            wxLogin.tryGetUserInfo({ id: userId, resolve })
          } else if (openId) {
            // 不存在
            resolve()
          } else {
            showToast({
              title: '网络走丢了'
            })
            resolve()
          }
        })
      } else {
        showToast({
          title: '网络走丢了'
        })
        resolve()
      }
    })
  },
  tryGetUserInfo: ({ id, resolve }) => {
    const { role } = wxLogin.props
    if (role == 'company') {
      wxLogin.getBUserInfo({ id, resolve })
    } else {
      wxLogin.getCUserInfo({ id, resolve })
    }
  },
  getCUserInfo: ({ id, resolve }) => {
    wxLogin.props.getZhichuUserInfo({ userId: id }).then(({ zhichuUserId }) => {
      if (zhichuUserId) {
      } else {
        wxLogin.props.updateUserState({
          userId: id
        })
        // showToast({
        //   title: '网络走丢了'
        // })
      }
      resolve()
    })
  },
  getBUserInfo: ({ id, resolve }) => {
    wxLogin.props.getZhichuBUserInfo({ userId: id }).then(({ zhichuUserIdB }) => {
      if (zhichuUserIdB) {
        wxLogin.doOnShow(resolve)
      } else {
        wxLogin.props.updateUserState({
          userId: id
        })
        // showToast({
        //   title: '网络走丢了'
        // })
        resolve()
      }
    })
  },
  doOnShow: resolve => {
    wxLogin.tryAsyncCompanyId(resolve)
  },
  tryAsyncCompanyId: resolve => {
    // b的userId role为company 且没有公司Id时尝试去同步公司id
    const { role, zhichuUserIdB } = wxLogin.props
    if (role == 'company' && zhichuUserIdB) {
      wxLogin.props.updateCompany()
    }
    resolve()
  },
  setPhoneModal: () => {
    getSystemInfo({
      success: res => {
        let brand = res.brand
        let isiPhone = false
        if (brand == 'iPhone') {
          isiPhone = true
        }
        setStorageSync('iPhone', isiPhone)
      }
    })
  },
  getArchiveInfo: () => {
    return new Promise(resolve => {
      // 获取用户资料   设置简历完成度
      getArchiveInfoRequest({}).then(({ data }) => {
        const { code, entry } = data
        if (code === 200) {
          // 设置简历完成度
          const { archiveComplete } = entry
          wxLogin.props.updateUserState({
            archiveComplete
          })
        }
        resolve()
      })
    })
  },
  refreshToken: token => {
    // 刷新token
    token = token || wxLogin.props.refreshToken
    return new Promise(resolve => {
      refreshTokenRequest({ refreshToken: token }).then(({ data = {} } = {}) => {
        const { code } = data
        const { access_token: accessToken, refresh_token: refreshToken } = data.data || {}
        if (code === 200) {
          wxLogin.props.updateUserState({
            accessToken,
            refreshToken
          })
          resolve({
            accessToken,
            refreshToken
          })
        } else {
          resolve({})
        }
      })
    })
  }
}

export default wxLogin
